package com.yiwyn.demo.servicer;


import com.yiwyn.demo.domain.User;
import com.yiwyn.demo.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Slf4j
@Service
public class TService {


    @Autowired
    private UserMapper userMapper;


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void beforeUpdate() {
        log.info("开始进行前置删除");
//        userMapper.deleteByPrimaryKey(1L);
//        userMapper.deleteByPrimaryKey(2L);
//        TransactionStatus transactionStatus = TransactionAspectSupport.currentTransactionStatus();
//        System.out.println("前置执行事务:" + transactionStatus);
        log.info("前置删除完成");
    }


        @Transactional(propagation = Propagation.REQUIRES_NEW)
//    @Transactional
    public void afterUpdate() {
        log.info("开始进行后置更新");
        User user = userMapper.selectByPrimaryKey(1L);
        user.setId(1);
        user.setUsername("updateName");
        userMapper.updateByPrimaryKeySelective(user);
        log.info("后置更新完成");
    }

}
